package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.Service;

import java.util.*;

/**
 * @author jshstart
 * @creat 2020-11-15 13:08
 */
@Service(timeout = 60000)
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public Boolean importData(List<String[]> data) {
        ArrayList<OrderSetting> list = new ArrayList<>();
        for(String[] row : data){
            String orderDate=row[0];
            String number=row[1];
            OrderSetting orderSetting = findOrderDate(orderDate);
            if (orderSetting==null){
                orderSetting=new OrderSetting();
                orderSetting.setOrderDate(DateUtil.parseDate(orderDate));
            }
            orderSetting.setNumber(Integer.parseInt(number));
            list.add(orderSetting);
        }
        this.saveOrUpdateBatch(list);
        return true;
    }

    @Override
    public boolean settingDialog(String settindDay, String number) {
        ArrayList<OrderSetting> list = new ArrayList<>();
        OrderSetting orderSetting = findOrderDate(settindDay);
        if (orderSetting==null){
            orderSetting=new OrderSetting();
            orderSetting.setOrderDate(DateUtil.parseDate(settindDay));
        }
        orderSetting.setNumber(Integer.parseInt(number));
        list.add(orderSetting);
        this.saveOrUpdateBatch(list);
        return true;
    }

    @Override
    public Map findAllByYearMonth(String year, String month) {
        HashMap<Object, Object> map = new HashMap<>();
        String yearMonth=year+"-"+(Integer.parseInt(month)<10?"0":"")+month;
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_format(order_date,'%Y-%m')",yearMonth);
        List<OrderSetting> orderSettings = this.list(queryWrapper);
        for (OrderSetting orderSetting : orderSettings) {
            map.put(DateUtil.formatDate(orderSetting.getOrderDate()),orderSetting);
        }
        return map;
    }

    @Override
    public OrderSetting isFull(String orderDate) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        OrderSetting orderSetting = this.getOne(queryWrapper);
        return orderSetting;
    }

    private OrderSetting findOrderDate(String orderDate){
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }
//定时清除过期数据
    @Override
    public  void clearHistoryObject() {
        int year = Integer.parseInt(DateUtil.formatDate(new Date(),"yyyy"));
        int month =Integer.parseInt(DateUtil.formatDate(new Date(),"MM"));
        System.out.println(year+"-"+month);
        String date =null;
        if (month>3){
            if (month-3<10){
                date=year+"-0"+(month-3);
            }else {
                date=year+"-"+(month-3);
            }

            System.out.println("date = " + date);
        }else {
            date=(year-1)+"-"+(month+9);
            System.out.println("date = " + date);

        }
        this.baseMapper.clearHistoryObject(date);
    }

    //根据日期查询订单
    @Override
    public OrderSetting findByDate(Date orderDate) {
        QueryWrapper<OrderSetting> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }

    //修改可预约数
    @Override
    public void updateReservations(OrderSetting orderSetting) {
        this.saveOrUpdate(orderSetting);
    }
}
